package com.jnu.student.myfirstapplication;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.jnu.student.myfirstapplication.data.DataDownload;
import com.jnu.student.myfirstapplication.data.ShopLocation;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdateFactory;
import com.tencent.tencentmap.mapsdk.maps.TencentMap;
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;
import com.tencent.tencentmap.mapsdk.maps.model.Marker;
import com.tencent.tencentmap.mapsdk.maps.model.MarkerOptions;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TencentMapFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TencentMapFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public TencentMapFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment TencentMapFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static TencentMapFragment newInstance(String param1, String param2) {
        TencentMapFragment fragment = new TencentMapFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    private com.tencent.tencentmap.mapsdk.maps.MapView mapView = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_tencent_map, container, false);
        mapView = rootView.findViewById(R.id.mapView);
        TencentMap tencentMap = mapView.getMap();
        LatLng point1 = new LatLng(22.255453, 113.54145);   // 指定定位数据
        tencentMap.moveCamera(CameraUpdateFactory.newLatLng(point1));       // 聚焦

        // 创建一个Marker对象
        MarkerOptions markerOptions = new MarkerOptions(point1).title("珠海暨南大学");       //  图形标记提示+文本标记提示
        // 添加标记到地图上
        Marker mCustomMarker = tencentMap.addMarker(markerOptions);
        //设置Marker支持点击
        mCustomMarker.setClickable(true);
        tencentMap.setOnMarkerClickListener(new TencentMap.OnMarkerClickListener(){
            @Override
            public boolean onMarkerClick(Marker marker) {
                if(marker.getId().equals(mCustomMarker.getId())) {
                    //自定义Marker被点击
                    Toast.makeText(requireActivity(), "标记了"+markerOptions.getTitle(), Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {
                String responseData=new DataDownload().download("http://file.nidama.net/class/mobile_develop/data/bookstore.json");
                ArrayList<ShopLocation> shopLocations= new DataDownload().parseJsonObjects(responseData);
                if (isAdded()) {
                    requireActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            TencentMap mtencentMap = mapView.getMap();
                            for (ShopLocation shopLocation : shopLocations) {
                                LatLng point2 = new LatLng(shopLocation.getLatitude(), shopLocation.getLongitude());
                                MarkerOptions markerOptions = new MarkerOptions(point2)
                                        .title(shopLocation.getName());
                                Marker marker = mtencentMap.addMarker(markerOptions);
                            }
                            if (isAdded()) {
                            Toast.makeText(requireActivity(), "已完成数据下载", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }
        }).start();
        return rootView;
    }
    public void onStart() {
        super.onStart();
        mapView.onStart();
    }
    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }
    @Override
    public void onStop() {
        super.onStop();
        mapView.onStop();
    }
    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
}